package com.highspeed.bdk.service.logic;

import com.highspeed.bdk.bean.entity.logic.LogicLink;
import com.highspeed.bdk.repository.logic.LogicLinkRepository;
import com.highspeed.bdk.service.base.BdkBaseService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenjiabin on 2018/3/23.
 *
 * @see ILogicLinkService
 */
@Service
@Transactional
public class LogicLinkService extends BdkBaseService<LogicLink, Integer> implements ILogicLinkService {
    private static final Log LOG = LogFactory.getLog(LogicLinkService.class);
    @Autowired
    private LogicLinkRepository physicalLinkRepository;

    /**
     * @see ILogicLinkService
     */
    @Override
    public LogicLinkRepository getRepository() {
        return physicalLinkRepository;
    }

    /**
     * @see ILogicLinkService
     */
    @Override
    public void deleteLinksByLogicTopologyId(Integer id) {
        LinkedHashMap<String, Object> params = new LinkedHashMap<String, Object>(0);
        params.put("leftApp.logicTopology.id_eq", id);
        params.put("rightApp.logicTopology.id_eq_or", id);
        List<LogicLink> links = queryToList(params, null);
        getRepository().deleteAll(links);
    }

    /**
     * @see ILogicLinkService
     */
    @Override
    public void deleteLinksByAppId(Integer id) {
        LinkedHashMap<String, Object> params = new LinkedHashMap<String, Object>(0);
        params.put("leftApp.id_eq", id);
        params.put("rightApp.id_eq_or", id);
        List<LogicLink> links = queryToList(params, null);
        getRepository().deleteAll(links);
    }


    /**
     * @see ILogicLinkService
     */
    @Override
    public List<LogicLink> findByLogicTopologyId(Integer id) {
        LinkedHashMap<String, Object> params = new LinkedHashMap<String, Object>(0);
        params.put("leftApp.logicTopology.id_eq", id);
        params.put("rightApp.logicTopology.id_eq_or", id);
        List<LogicLink> links = queryToList(params, null);
        return links;
    }


    /**
     * @see ILogicLinkService
     */
    @Override
    public List<LogicLink> findByAppId(Integer id) {
        LinkedHashMap<String, Object> params = new LinkedHashMap<String, Object>(0);
        params.put("leftApp.id_eq", id);
        params.put("rightApp.id_eq_or", id);
        List<LogicLink> links = queryToList(params, null);
        return links;
    }

    @Override
    public LogicLink findByTwoAppId(Integer leftAppId, Integer rightAppId) {
        LinkedHashMap<String, Object> params = new LinkedHashMap<String, Object>(0);
        params.put("leftApp.id_eq", leftAppId);
        params.put("rightApp.id_eq", rightAppId);
        List<LogicLink> links = queryToList(params, null);
        if (links != null && !links.isEmpty()) {
            return links.get(0);
        }
        return null;
    }

    @Override
    public List<LogicLink> findAllByTwoAppId(Integer leftAppId, Integer rightAppId) {
        LinkedHashMap<String, Object> params = new LinkedHashMap<String, Object>(0);
        params.put("leftApp.id_eq", leftAppId);
        params.put("rightApp.id_eq", rightAppId);
        List<LogicLink> links = queryToList(params, null);
        return links;
    }
}
